package com.loongtech.bi.action.system;

import com.loongtech.bi.entity.system.EntitySysRole;
import com.loongtech.bi.manager.system.AdvConfigManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.manager.system.SysRoleAdManager;
import com.loongtech.bi.manager.system.SysRoleManager;
import com.loongtech.bi.support.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.*;

/**
 * 角色代理商设置
 */
@Controller("roleAdManager")
@Scope("view")
public class RoleAdManager implements Serializable
{
	private static final long			serialVersionUID	= 1L;
	@Autowired
	private Session						session;
	@Autowired
	private SysLogManager				sysLogManager;
	@Autowired
	private SysRoleManager				sysRoleManager;
	@Autowired
	private SysRoleAdManager sysRoleAdManager;
	private Map<String, Boolean>		selected			= new HashMap<>();
	private Map<String, Boolean>		unselected			= new HashMap<>();
	private int							roleId;
	private Map<Integer, EntitySysRole>	roleMap				= new HashMap<>();
	private List<String>		leftList			= new ArrayList<>();		// 可选择列表
	private List<String>		rightList			= new ArrayList<>();		// 已经选择列表

	@Autowired
	public void init()
	{
		roleId = 0;
		roleMap.clear();
		for (EntitySysRole role : sysRoleManager.findAll())
		{
			roleMap.put(role.getId(), role);
		}
		change();
	}

	public void select()
	{
		for (String ad : leftList)
		{
			Boolean val = selected.get(ad);
			if (null != val && val.booleanValue())
			{
				rightList.add(ad);
			}
		}
		selected.clear();
		leftList.removeAll(rightList);
	}

	public void unSelect()
	{
		for (String ad : rightList)
		{
			Boolean val = unselected.get(ad);
			if (null != val && val.booleanValue())
			{
				leftList.add(ad);
			}
		}
		unselected.clear();
		rightList.removeAll(leftList);
	}

	/**
	 * 保存角色服务器信息
	 */
	public void save()
	{
		FacesContext context = FacesContext.getCurrentInstance();
		EntitySysRole role = sysRoleManager.get(roleId);
		if (null == role)
		{
			context.addMessage(null, new FacesMessage("修改角色代理商列表失败", "修改角色代理商列表失败，角色不存在 roleId=" + roleId));
			return;
		}
		// 当前集合
		Set<String> oldSet = sysRoleAdManager.getRoleAdSet(roleId);

		// newSet of id
		Set<String> newSet = getSet(rightList);
		Set<String> submit = new HashSet<>(oldSet);
		submit.retainAll(newSet);
		oldSet.removeAll(submit); // to remove
		newSet.removeAll(submit); // to insert
		// 删除oldSet
		for (String channelId : oldSet)
		{
			sysRoleAdManager.removeByRoleIdAndAd(roleId, channelId);
		}
		// 插入newSet
		for (String channelId : newSet)
		{
            sysRoleAdManager.insertByRoleIdAndAd(roleId, channelId);
		}
		sysLogManager.addLog(session.getUserName(), "修改角色代理商列表成功" + role.getName());
		context.addMessage(null, new FacesMessage("修改角色代理商列表成功", "修改角色代理商列表成功，角色名称为=" + role.getName()));
		change();
	}

	private Set<String> getSet(List<String> list)
	{
		Set<String> curSet = new HashSet<>();
		if (null == list || list.isEmpty())
		{
			return curSet;
		}
		for (String info : list)
		{
				curSet.add(info);
		}
		return curSet;
	}

	public void change()
	{
		selected.clear();
		unselected.clear();
		EntitySysRole role = sysRoleManager.get(roleId);
		if (null == role)
		{
			leftList.clear();
			rightList.clear();
			leftList.addAll(sysRoleAdManager.getRoleAdSet(session.getUserId()));
			return;
		}
		// 先初始化rightList
		rightList.clear();
		rightList = sysRoleAdManager.getRoleAdList(roleId);
		leftList.clear();
		leftList.addAll(AdvConfigManager.AD_LIST);
		leftList.removeAll(rightList);
	}

	public int getRoleId()
	{
		return roleId;
	}

	public void setRoleId(int roleId)
	{
		this.roleId = roleId;
	}

	public Map<Integer, EntitySysRole> getRoleMap()
	{
		return roleMap;
	}

	public void setRoleMap(Map<Integer, EntitySysRole> roleMap)
	{
		this.roleMap = roleMap;
	}

    public Map<String, Boolean> getSelected() {
        return selected;
    }

    public void setSelected(Map<String, Boolean> selected) {
        this.selected = selected;
    }

    public Map<String, Boolean> getUnselected() {
        return unselected;
    }

    public void setUnselected(Map<String, Boolean> unselected) {
        this.unselected = unselected;
    }

    public List<String> getLeftList() {
        return leftList;
    }

    public void setLeftList(List<String> leftList) {
        this.leftList = leftList;
    }

    public List<String> getRightList() {
        return rightList;
    }

    public void setRightList(List<String> rightList) {
        this.rightList = rightList;
    }
}
